package com.zzt.lotterysystem.service.activitystatus.operator;

import com.zzt.lotterysystem.dao.dataobject.ActivityUserDO;
import com.zzt.lotterysystem.dao.mapper.ActivityUserMapper;
import com.zzt.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.zzt.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-05-29
 * Time: 16:22
 */
@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if (null == activityId || CollectionUtils.isEmpty(userIds) || null == targetActivityStatus) {
            return false;
        }

        List<ActivityUserDO> activityUserDOList = activityUserMapper.selectByActivityIdAndUserId(activityId, userIds);
        if (CollectionUtils.isEmpty(activityUserDOList)) {
            return false;
        }
        for (ActivityUserDO activityUserDO : activityUserDOList) {
            if (activityUserDO.getStatus()
                    .equalsIgnoreCase(targetActivityStatus.name())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId, userIds, targetActivityStatus.name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
